package com.tanhua.common.utils.oss;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.tanhua.common.pojo.PicUploadResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

//文件上传阿里云工具类
public class OssWarehouse {

    // 允许上传的格式
    private static final String[] IMAGE_TYPE = new String[]{".bmp", ".jpg",
            ".jpeg", ".gif", ".png"};

    static String bucketName = "mytanhua52";
    static String endpoint = "https://oss-cn-beijing.aliyuncs.com";
    static String accessKeyId = "LTAI5tSoLnXEDhrwmnJjwk7n";
    static String accessKeySecret = "MSXsvCZyhEGNHbKK1pz5sSOt46HlRK";
    static String urlPrefix = "https://mytanhua52.oss-cn-beijing.aliyuncs.com/";


    //上传
    public static PicUploadResult uploadFiles(byte[] pic, String fileName) {

        // 返回一个对象 标记这次上传是否成功了
        PicUploadResult fileUploadResult = new PicUploadResult();

        boolean isLegal = false;

        // 校验文件的后缀名
        for (String type : IMAGE_TYPE) {
            if (StringUtils.endsWithIgnoreCase(fileName, type)) {
                isLegal = true;
                break;
            }
        }

        if (!isLegal) {
            fileUploadResult.setStatus("error");
            return fileUploadResult;
        }
        //long size = uploadFile.getSize();/1024/1024得到m单位
//        long l = uploadFile.getSize() / 1024 / 1024;
//        if (l > 5) {
//            fileUploadResult.setStatus("文件太大不支持上传");
//            return fileUploadResult;
//        }


        //获取文件的原始名称 lufei.jpeg
//        String fileName = uploadFile.getOriginalFilename();

        // 生成新的文件名称 /images/yyyy/MM/dd/xxxxxxx.jpg
        String filePath = DirUtil.getFilePath(fileName);


        //        算法工具类（可以更换）
        //        String filePath = DirUtil.getRandomDir(fileName);

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
//            ossClient.putObject(bucketName, filePath, new ByteArrayInputStream(uploadFile.getBytes()));
            ossClient.putObject(bucketName, filePath, new ByteArrayInputStream(pic));
        } catch (Exception e) {
            e.printStackTrace();
            //上传失败
            fileUploadResult.setStatus("error");
            return fileUploadResult;
        }
        // 上传成功
        fileUploadResult.setStatus("success");
        fileUploadResult.setName(urlPrefix + filePath);
        fileUploadResult.setUid(String.valueOf(System.currentTimeMillis()));
        // 关闭OSSClient。
        ossClient.shutdown();
        return fileUploadResult;
    }


    //字节流啥都能传
    public static PicUploadResult uploadFiles(InputStream inputStream,String fileName) {
        // 返回一个对象 标记这次上传是否成功了
        PicUploadResult fileUploadResult = new PicUploadResult();
        try {
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            // 生成新的文件名称 /images/yyyy/MM/dd/xxxxxxx.jpg
            String filePath = DirUtil.getFilePath(fileName);
            ossClient.putObject(bucketName, filePath, inputStream);
            fileUploadResult.setName(urlPrefix + filePath);
            // 上传成功
            fileUploadResult.setStatus("success");
            fileUploadResult.setUid(String.valueOf(System.currentTimeMillis()));
            // 关闭OSSClient。
            ossClient.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
            fileUploadResult.setStatus("失败");
            return fileUploadResult;
        }
        return fileUploadResult;
    }

}
